package com.amqp.demo;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.Console;
import java.util.Scanner;

public class Producer {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
        //AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
        RabbitTemplate amqpTemplate = (RabbitTemplate)context.getBean(AmqpTemplate.class);
        //amqpTemplate.setExchange("amq.direct"); // 直接 交换
        //amqpTemplate.setExchange("amq.fanout"); // 广播 交换
        amqpTemplate.setExchange("amq.rabbitmq.trace"); // 主题交换
        //amqpTemplate.setRoutingKey("100");
        amqpTemplate.setMessageConverter(new Jackson2JsonMessageConverter());

        //启动消息失败返回，比如路由不到队列时触发回调
//        ((CachingConnectionFactory)amqpTemplate.getConnectionFactory()).setPublisherReturns(true);
//        amqpTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback(){
//
//            @Override
//            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
//                System.out.println("消息主体 message : "+message);
//                System.out.println("消息主体 message : "+replyCode);
//                System.out.println("描述："+replyText);
//                System.out.println("消息使用的交换器 exchange : "+exchange);
//                System.out.println("消息使用的路由键 routing : "+routingKey);
//            }
//        });
        // 消息发送结果查看
//        ((CachingConnectionFactory)amqpTemplate.getConnectionFactory()).setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
//        amqpTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback(){
//
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                System.out.println("消息唯一标识："+correlationData);
//                System.out.println("确认结果："+ack);
//                System.out.println("失败原因："+cause);
//            }
//        });
        System.out.println("\n RabbitMQ连接成功，请输入消息，输入exit退出！");
        Scanner scanner = new Scanner(System.in);
        String input;
        do {
            input = scanner.nextLine();
            //byte[] sendBytes = input.getBytes();
            //amqpTemplate.convertAndSend(sendBytes);
            amqpTemplate.convertAndSend("1.sss.1",new Order(input,2.015,100L));
            System.out.println("Sent");
        }while (input.toLowerCase().trim() != "exit");
        System.out.println("\n RabbitMQ exit！！！");

    }
}
